package com.mgl.movesprite.game.factory;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;

import android.content.res.AssetManager;

public final class GameTextureManager {

	private TextureManager textureManager;
	private AssetManager assetManager;
	private VertexBufferObjectManager vertexBufferObjectManager;

	// textures
	// background level
	// textures-------------------------------------------------------
	private ITextureRegion backgroundTexture;

	// monster textures
	// --------------------------------------------------------------
	private ITextureRegion monsterTexture;

	// tower textures
	// ----------------------------------------------------------------
	private ITextureRegion towerTexture;
	private ITextureRegion buildZoneTexture;
	private ITextureRegion towerFireTexture;
	private ITextureRegion towerWaterTexture;
	// shot textures
	// -----------------------------------------------------------------
	private ITextureRegion shotTexture;

	// general textures
	// --------------------------------------------------------------
	private ITextureRegion beginPointTexture;
	private ITextureRegion endPointTexture;
	private ITextureRegion blackBackgroundTexture;
	private ITextureRegion radioTexture;
	
	//buttons Textures
	private ITextureRegion pauseButtonTexture;
	private ITextureRegion closeButtonTexture;
	private ITextureRegion nextButtonTexture;
	private ITextureRegion prevButtonTexture;
	
	//Tiled texture Region
	private ITiledTextureRegion monsterITextureRegion;
	private BitmapTextureAtlas monsterAtlas;
	

	public GameTextureManager(TextureManager textureManager,
			final AssetManager assetManager,
			VertexBufferObjectManager vertexBufferObjectManager) {

		this.textureManager = textureManager;
		this.assetManager = assetManager;
		this.vertexBufferObjectManager = vertexBufferObjectManager;

	}

	private ITextureRegion loadTexture(final String name) {
		try {

			ITexture iTexture = new BitmapTexture(textureManager,
					new IInputStreamOpener() {
						@Override
						public InputStream open() throws IOException {
							return assetManager.open("gfx/" + name);
						}
					});
			iTexture.load();

			return TextureRegionFactory.extractFromTexture(iTexture);

		} catch (IOException e) {
			Debug.e(e);
			return null;
		}

	}

	public void loadTexture() {

		try {
			loadMonsterTextures();
			loadTowerTextures();
			loadShotTextures();
			loadBackgroundLevelTextures();
			loadGeneralTextures();
			loadButtonTextures();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void loadButtonTextures() {
		
		closeButtonTexture = loadTexture("close.png");
		pauseButtonTexture = loadTexture("pause.png");
		nextButtonTexture = loadTexture("next.png");
		prevButtonTexture = loadTexture("prev.png");
	}

	private void loadGeneralTextures() {

		beginPointTexture = loadTexture("woodSign.png");
		endPointTexture = loadTexture("woodSign.png");
		blackBackgroundTexture = loadTexture("blackBackground.png");
		radioTexture = loadTexture("radio.png");

	}

	private void loadBackgroundLevelTextures() {
		backgroundTexture = loadTexture("camino1.png");

	}

	private void loadShotTextures() {
		shotTexture = loadTexture("shot.png");

	}

	private void loadTowerTextures() {
		
		towerTexture = loadTexture("torre.png");
		buildZoneTexture = loadTexture("buildZone.png");
		towerWaterTexture = loadTexture("towerWater.png");
		towerFireTexture = loadTexture("towerFire.png");

	}

	private void loadMonsterTextures() {

		monsterTexture = loadTexture("globo.gif");

		monsterAtlas = new BitmapTextureAtlas(getTextureManager(), 96, 128,TextureOptions.BILINEAR);
		monsterITextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(monsterAtlas,getAssetManager(), "gfx/bad1.png", 0, 0,3,4);
		monsterAtlas.load();
	}

	public TextureManager getTextureManager() {
		return textureManager;
	}

	public void setTextureManager(TextureManager textureManager) {
		this.textureManager = textureManager;
	}

	public AssetManager getAssetManager() {
		return assetManager;
	}

	public void setAssetManager(AssetManager assetManager) {
		this.assetManager = assetManager;
	}

	public ITextureRegion getMonsterTexture() {
		return monsterTexture;
	}

	public void setMonsterTexture(ITextureRegion monsterTexture) {
		this.monsterTexture = monsterTexture;
	}

	public ITextureRegion getTowerTexture() {
		return towerTexture;
	}

	public void setTowerTexture(ITextureRegion towerTexture) {
		this.towerTexture = towerTexture;
	}

	public ITextureRegion getShotTexture() {
		return shotTexture;
	}

	public void setShotTexture(ITextureRegion shotTexture) {
		this.shotTexture = shotTexture;
	}

	public ITextureRegion getBackgroundTexture() {
		return backgroundTexture;
	}

	public void setBackgroundTexture(ITextureRegion backgroundTexture) {
		this.backgroundTexture = backgroundTexture;
	}

	public VertexBufferObjectManager getVertexBufferObjectManager() {
		return vertexBufferObjectManager;
	}

	public void setVertexBufferObjectManager(
			VertexBufferObjectManager vertexBufferObjectManager) {
		this.vertexBufferObjectManager = vertexBufferObjectManager;
	}

	public ITextureRegion getBeginPointTexture() {
		return beginPointTexture;
	}

	public void setBeginPointTexture(ITextureRegion beginPointTexture) {
		this.beginPointTexture = beginPointTexture;
	}

	public ITextureRegion getEndPointTexture() {
		return endPointTexture;
	}

	public void setEndPointTexture(ITextureRegion endPointTexture) {
		this.endPointTexture = endPointTexture;
	}

	public ITextureRegion getPauseButtonTexture() {
		return pauseButtonTexture;
	}

	public void setPauseButtonTexture(ITextureRegion pauseButtonTexture) {
		this.pauseButtonTexture = pauseButtonTexture;
	}

	public ITextureRegion getBuildZoneTexture() {
		return buildZoneTexture;
	}

	public void setBuildZoneTexture(ITextureRegion buildZoneTexture) {
		this.buildZoneTexture = buildZoneTexture;
	}

	public ITextureRegion getBlackBackgroundTexture() {
		return blackBackgroundTexture;
	}

	public void setBlackBackgroundTexture(ITextureRegion blackBackgroundTexture) {
		this.blackBackgroundTexture = blackBackgroundTexture;
	}

	public ITextureRegion getCloseButtonTexture() {
		return closeButtonTexture;
	}

	public void setCloseButtonTexture(ITextureRegion closeButtonTexture) {
		this.closeButtonTexture = closeButtonTexture;
	}

	public ITextureRegion getTowerFireTexture() {
		return towerFireTexture;
	}

	public void setTowerFireTexture(ITextureRegion towerFireTexture) {
		this.towerFireTexture = towerFireTexture;
	}

	public ITextureRegion getTowerWaterTexture() {
		return towerWaterTexture;
	}

	public void setTowerWaterTexture(ITextureRegion towerWaterTexture) {
		this.towerWaterTexture = towerWaterTexture;
	}

	public ITextureRegion getNextButtonTexture() {
		return nextButtonTexture;
	}

	public void setNextButtonTexture(ITextureRegion nextButtonTexture) {
		this.nextButtonTexture = nextButtonTexture;
	}

	public ITextureRegion getPrevButtonTexture() {
		return prevButtonTexture;
	}

	public void setPrevButtonTexture(ITextureRegion prevButtonTexture) {
		this.prevButtonTexture = prevButtonTexture;
	}

	public ITextureRegion getRadioTexture() {
		return radioTexture;
	}

	public void setRadioTexture(ITextureRegion radioTexture) {
		this.radioTexture = radioTexture;
	}

	public ITiledTextureRegion getMonsterITextureRegion() {
		return monsterITextureRegion;
	}

	public void setMonsterITextureRegion(ITiledTextureRegion monsterITextureRegion) {
		this.monsterITextureRegion = monsterITextureRegion;
	}

	public BitmapTextureAtlas getMonsterAtlas() {
		return monsterAtlas;
	}

	public void setMonsterAtlas(BitmapTextureAtlas monsterAtlas) {
		this.monsterAtlas = monsterAtlas;
	}
		
}
